package com.yuncheng.oaApi.service.impl;

import org.apache.commons.collections.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuncheng.oaApi.entity.OaSysUserFgld;
import com.yuncheng.entity.YcSysOrg;
import com.yuncheng.entity.YcSysUser;
import com.yuncheng.oaApi.mapper.OaSysUserFgldMapper;
import com.yuncheng.oaApi.service.IOaSysUserFgldService;
import com.yuncheng.oaApi.service.IOaYcSysOrgService;
import com.yuncheng.oaApi.service.IOaYcSysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.yuncheng.common.base.service.impl.BaseServiceImpl;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: 系统-分管领导配置
 * @Author: csx
 * @Date: 2023-03-03
 * @Version: V1.0
 */
@Service
public class OaSysUserFgldServiceImpl extends BaseServiceImpl<OaSysUserFgldMapper, OaSysUserFgld> implements IOaSysUserFgldService {

    @Resource
    private IOaYcSysOrgService ycSysOrgService;

    @Resource
    private IOaYcSysUserService ycSysUserService;

    @Override
    public YcSysUser getFgldUserBydept(String deptName) {
        if (StringUtils.isEmpty(deptName)){
            return null;
        }
        // 获取部门
        List<YcSysOrg> deptList = ycSysOrgService.list(new LambdaQueryWrapper<YcSysOrg>()
                .like(YcSysOrg::getOrgName, deptName));
        if (CollectionUtils.isNotEmpty(deptList)){
            YcSysOrg ycSysOrg = deptList.get(0);
            String deptId = ycSysOrg.getId();

            // 获取部门的分管领导用户
            List<OaSysUserFgld> fgldList = list(new LambdaQueryWrapper<OaSysUserFgld>()
                    .eq(OaSysUserFgld::getFdeptid, deptId));
            if (CollectionUtils.isNotEmpty(fgldList)){
                OaSysUserFgld oaSysUserFgld = fgldList.get(0);
                String fuserId = oaSysUserFgld.getFuserid();

                // 获取用户
                YcSysUser sysUser = ycSysUserService.getById(fuserId);
                return sysUser;
            }
        }

        return null;
    }
}
